﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Media;
using System.Windows.Controls;
using System.Windows.Documents;

namespace Pavilion.DesignerTool
{
    public static class PavilionVisualTreeHelper
    {
        public static T GetParentOfContextMenu<T>(MenuItem menuItem) where T : DependencyObject
        {
            return GetParentOfContextMenu<T>(menuItem.Parent as ContextMenu);
        }

        public static T GetParentOfContextMenu<T>(ContextMenu contextMenu) where T : DependencyObject
        {
            return contextMenu.PlacementTarget as T;
        }

        public static T GetAncestor<T>(DependencyObject startingElement) where T : DependencyObject
        {
            DependencyObject parent = VisualTreeHelper.GetParent(startingElement);

            if (parent == null || parent is T || parent.GetType().BaseType == typeof(T))
                return parent as T;
            else
                return GetAncestor<T>(parent);
        }

        public static T GetChild<T>(DependencyObject startingElement) where T : DependencyObject
        {
            if (startingElement == null || startingElement is T)
                return startingElement as T;
            else
            {
                int count = VisualTreeHelper.GetChildrenCount(startingElement);

                for (int i = 0; i < count; i++)
                {
                    DependencyObject child = VisualTreeHelper.GetChild(startingElement, i) as DependencyObject;

                    child = GetChild<T>(child);

                    if (child != null)
                        return child as T;
                }

                return null;
            }
        }

        /// <summary>
        /// Returns parent of starting element that has an immediate parent of the passed in type. 
        /// </summary>       
        /// <param name="type">parent type of desired element</param>
        public static DependencyObject GetParentWhoseAncestorIsType(DependencyObject startingElement, Type type)
        {
            if (startingElement == null || VisualTreeHelper.GetParent(startingElement).GetType() == type)
                return startingElement;
            else
               return GetParentWhoseAncestorIsType(VisualTreeHelper.GetParent(startingElement), type);
        }

        public static T FindItemsPanel<T>(Visual visual)
        {
            for (int i = 0; i < VisualTreeHelper.GetChildrenCount(visual); i++)
            {
                Visual child = VisualTreeHelper.GetChild(visual, i) as Visual;

                if (child != null)
                {
                    if (child is T && VisualTreeHelper.GetParent(child) is ItemsPresenter)
                    {
                        object temp = child;
                        return (T)temp;
                    }

                    T panel = FindItemsPanel<T>(child);

                    if (panel != null)
                    {
                        object temp = panel;
                        return (T)temp; // return the panel up the call stack
                    }
                }
            }

            return default(T);
        }

        /// <typeparam name="T">Type of Adorner to Remove</typeparam>
        public static void RemoveAdorners<T>(UIElement adornedElement) where T:Adorner
        {
            var adornerLayer = AdornerLayer.GetAdornerLayer(adornedElement);
            var adorners = adornerLayer.GetAdorners(adornedElement);
            if (adorners == null)
                return;

            foreach (var adorner in adorners)
            {
                if (adorner is T)
                    adornerLayer.Remove(adorner);
            }
        
        }
    }

    #region VisualTreeWalker

    public delegate void VisualVisitedEventHandler(Object sender, VisualVisitedEventArgs e);

    public class VisualVisitedEventArgs : EventArgs
    {
        public Visual VisitedVisual { get; private set; }
        public Int32 CurrentDepth { get; private set; }

        public VisualVisitedEventArgs(Visual visitedVisual, Int32 currentDepth)
        {
            this.VisitedVisual = visitedVisual;
            this.CurrentDepth = currentDepth;
        }
    }

    public class VisualTreeWalker
    {
        private Int32 visualCount;
        public event VisualVisitedEventHandler VisualVisited;

        public Int32 Walk(Visual rootVisual)
        {
            this.visualCount = 0;
            this.TraverseVisuals(rootVisual, 1);
            return this.visualCount;
        }

        private void TraverseVisuals(Visual visual, Int32 currentDepth)
        {
            this.visualCount++;
            this.OnVisualVisited(new VisualVisitedEventArgs(visual, currentDepth));

            for (Int32 i = 0; i < VisualTreeHelper.GetChildrenCount(visual); i++)
            {
                Visual child = VisualTreeHelper.GetChild(visual, i) as Visual;
                this.TraverseVisuals(child, currentDepth++);
            }
        }

        protected void OnVisualVisited(VisualVisitedEventArgs e)
        {
            if (this.VisualVisited != null)
            {
                this.VisualVisited(this, e);
            }
        }
    }

    #endregion
}
